package v3.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import v3.annotion.DataSource;
import v3.common.exception.BadRequestException;
import v3.common.object.PageObject;
import v3.common.object.QueryParamObject;
import v3.common.util.FuncUtil;
import v3.common.util.JwtUtil;
import v3.entity.User;
import v3.service.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("list")
    public ResponseEntity<User> getList(PageObject page, QueryParamObject queryParamObject){
        log.info("test:"+page.toString()+queryParamObject.toString());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<User> list =  userService.getList(queryParamObject);
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        System.out.println(pageInfo);
        return  new ResponseEntity(pageInfo,HttpStatus.OK);
    }
    @DataSource(name = "my123456")
    @RequestMapping("detail")
    public ResponseEntity<User> detail(@RequestParam("userCode") String userCode){
        User detail =  userService.detail(userCode);
        return  new ResponseEntity(detail,HttpStatus.OK);
    }
    @PostMapping("add")
    public ResponseEntity<User> add(@Validated @RequestBody User user,BindingResult bindingResult){
        FuncUtil.validForm(user,bindingResult);
        User detail =  userService.add(user);
        return  new ResponseEntity(detail,HttpStatus.OK);
    }
    @PostMapping("update")
    public ResponseEntity<User> update(@Validated @RequestBody User user,BindingResult bindingResult){
        FuncUtil.validForm(user,bindingResult);
        User detail =  userService.update(user);
        return  new ResponseEntity(detail,HttpStatus.OK);
    }
    @PostMapping("delete")
    public ResponseEntity<User> delete(@RequestBody User user){
        userService.delete(user);
        return  new ResponseEntity("ok",HttpStatus.OK);
    }
    @PostMapping("login")
    public ResponseEntity<User> login(@RequestBody User user){
        User userInfo = userService.login(user);
        String token = JwtUtil.sign(userInfo.getUserId(),userInfo.getUserPwd());
        Map<String, Object> map = new HashMap<>();
        map.put("userInfo",userInfo);
        map.put("token",token);
        return  new ResponseEntity(map,HttpStatus.OK);
    }


    @RequestMapping("test")
    public ResponseEntity<User> getList2 (QueryParamObject queryParamObject){
        List<User> list =  userService.getList(queryParamObject);
          throw new BadRequestException("this is a");

    }
}
